<template>


	<view class="text-area">

		<uni-data-checkbox v-model="wxMerchant" :localdata="wx"></uni-data-checkbox>


		<view v-show="wxMerchant == '2'">
			<uni-forms :rules="rulesWxForm" ref="wxForm" :modelValue="wxForm" label-position="top">




				<uni-forms-item label="微信appid" name="appid" required labelWidth="180px">
					<uni-easyinput v-model="wxForm.appid" placeholder="请输入微信appid" />

				</uni-forms-item>
				<uni-forms-item label="直连商户号" name="mchid" required labelWidth="180px">
					<uni-easyinput v-model="wxForm.mchid" placeholder="请输入直连商户号" />

				</uni-forms-item>
				<uni-forms-item label="APIV3密钥" name="appV3Key" required labelWidth="180px">
					<uni-easyinput v-model="wxForm.appV3Key" placeholder="请输入APIV3密钥" />

				</uni-forms-item>
				<uni-forms-item label="证书序列号" name="merchantSerialNumber" required labelWidth="180px">
					<uni-easyinput v-model="wxForm.merchantSerialNumber" placeholder="请输入证书序列号" />

				</uni-forms-item>
				<uni-forms-item label="私钥" name="privateKeyPath" required labelWidth="180px">
					<uni-easyinput v-model="wxForm.privateKeyPath" placeholder="请输入私钥" />

				</uni-forms-item>
				<view class="sumbit-btn flex" @click.stop="saveInfo('wxForm')">
					保存信息
				</view>


			</uni-forms>
		</view>
		<view v-show="wxMerchant == '1'">
			<uni-forms :rules="rulesWxServiceForm" ref="wxServiceForm" :modelValue="wxServiceForm" label-position="top">




				<uni-forms-item label="服务商应用ID" name="spAppid" required labelWidth="180px">
					<uni-easyinput v-model="wxServiceForm.spAppid" placeholder="请输入服务商应用ID" />

				</uni-forms-item>
				<uni-forms-item label="服务商户号" name="spMchid" required labelWidth="180px">
					<uni-easyinput v-model="wxServiceForm.spMchid" placeholder="请输入服务商户号" />

				</uni-forms-item>
				<uni-forms-item label="子商户号" name="subMchid" required labelWidth="180px">
					<uni-easyinput v-model="wxServiceForm.subMchid" placeholder="请输入子商户号" />

				</uni-forms-item>
				<uni-forms-item label="APIv3密钥" name="appV3Key" required labelWidth="180px">
					<uni-easyinput v-model="wxServiceForm.appV3Key" placeholder="请输入APIv3密钥" />

				</uni-forms-item>
				<uni-forms-item label="证书序列号" name="merchantSerialNumber" required labelWidth="180px">
					<uni-easyinput v-model="wxServiceForm.merchantSerialNumber" placeholder="请输入私钥" />

				</uni-forms-item>
				<view class="sumbit-btn flex" @click.stop="saveInfo('wxServiceForm')">
					保存信息
				</view>


			</uni-forms>
		</view>
	</view>

</template>

<script>
	export default {

		data() {
			return {
				title: 'paymentConfiguration',
				wx: [{
					text: 'V3商户支付',
					value: '2'
				}, {
					text: '特约商户支付',
					value: '1'
				}],
				merchantId:'',
				formArr: ['wxForm', 'wxServiceForm', 'zfbForm', 'zfbServiceForm'],
				//   V3商户支付
				wxForm: {
					appid: '', //appid
					mchid: '', //直连商户号

					appV3Key: '', //APIv3密钥
					merchantSerialNumber: '', //证书序列号
					privateKeyPath: '', // 私钥
				},

				//   特约子商户支付
				wxServiceForm: {
					spAppid: '', // 服务商应用ID
					spMchid: '', //服务商户号
					subMchid: '', //子商户号
					appV3Key: '', //APIv3密钥
					merchantSerialNumber: '', //证书序列号
					privateKeyPath: '', //私钥
				},
				 wxPayId: '',
				 wxSpPayId: '',
				 zfbPayId: '',
				 zfbSpPayId: '',
				
				payType: '1',
				wxMerchant: '2',
				zfbMerchant: '2',
				rulesWxForm: {
					appid: {
						rules: [{
							required: true,
							errorMessage: '微信appid不能为空'
						}]
					},
					mchid: {
						rules: [{
							required: true,
							errorMessage: '直连商户号不能为空'
						}]
					},
					appV3Key: {
						rules: [{
							required: true,
							errorMessage: 'APIv3密钥不能为空'
						}]
					},
					merchantSerialNumber: {
						rules: [{
							required: true,
							errorMessage: '证书序列号不能为空'
						}]
					},
					privateKeyPath: {
						rules: [{
							required: true,
							errorMessage: '私钥不能为空'
						}]
					}
				},
				rulesWxServiceForm:{
					spAppid: {
						rules: [{
							required: true,
							errorMessage: '服务商应用ID不能为空'
						}]
					},
					spMchid: {
						rules: [{
							required: true,
							errorMessage: '服务商户号不能为空'
						}]
					},
					subMchid: {
						rules: [{
							required: true,
							errorMessage: '子商户号不能为空'
						}]
					},
					appV3Key: {
						rules: [{
							required: true,
							errorMessage: 'APIv3密钥不能为空'
						}]
					},
					merchantSerialNumber: {
						rules: [{
							required: true,
							errorMessage: '证书序列号不能为空'
						}]
					},
					privateKeyPath: {
						rules: [{
							required: true,
							errorMessage: '私钥不能为空'
						}]
					}
				}
			}
		},
		mounted () {
		 this.searchForm()
		  },
	
		methods: {

			saveInfo(formName) {
				console.log(formName)
				this.$refs[formName].validate((err, formData) => {
					// 如果校验成功 ，err 返回 null
					if (!err) {
						console.log('success', formData)
						return
					}
					console.log('error', err)
				}).then(async res => {
					console.log(res)
					// res 返回 null
					for (let i = 0; i < 4; i++) {
						if (!(formName == this.formArr[i])) {
							this.resetForm(this.formArr[i]);
						}
					}

					// 保存的时候存最后保存的sign 和 payType
					console.log((this.payType == '1' && this.wxMerchant == '1'))
					const sumbitForm = this.currentFormFn(this.payType)
					localStorage.setItem('payType', JSON.stringify({
						sign: sumbitForm[0][sumbitForm[1]].sign,
						payType: sumbitForm[0][sumbitForm[1]].type
					}));
					console.log(sumbitForm[0])
					
					// const result = await this.$http.post({
					// 	url: this.$api.insertSelective,
					// 	data: {
					// 		...sumbitForm[0]
					// 	},
					
					
					// });
					// this.$message.success(res.msg);
					//  this.searchForm()
				
				})

			},
			resetForm(formName) {
				// console.log(formName)
				// console.log(this.$refs[formName])
				// this.$refs[formName].resetFields();
			},
			
			
			async searchForm () {
					
					
					const res = await this.$http.post({
						url: this.$api.selectByPrimaryMerchantId,
						data: {
							merchantId: this.merchantId,
						},
					
					
					});
			     
			      console.log(res)
			      // 获取到微信支付的id
			      this.wxPayId = res.data.wxInfo?.id
			      this.wxSpPayId = res.data.wxSpInfo?.id
			      this.zfbPayId = res.data.zfbInfo?.id
			      this.zfbSpPayId = res.data.zfbSpInfo?.id
			      if (res.data.wxInfo?.type == '1' && res.data.wxInfo?.sign == '2') {
			        this.findMatchingProperties(res.data.wxInfo
			          , this.wxForm)
			      }
			      if (res.data.wxSpInfo?.type == '1' && res.data.wxSpInfo?.sign == '1') {
			        this.findMatchingProperties(res.data.wxSpInfo
			          , this.wxServiceForm)
			      }
			      if (res.data.zfbInfo?.type == '2' && res.data.zfbInfo?.sign == '2') {
			        this.findMatchingProperties(res.data.zfbInfo
			          , this.zfbForm)
			      }
			      if (res.data.zfbSpInfo?.type == '2' && res.data.zfbSpInfo?.sign == '1') {
			        this.findMatchingProperties(res.data.zfbSpInfo
			          , this.zfbServiceForm)
			      }
			      if (JSON.parse(localStorage.getItem('payType'))) {
			        const sign = JSON.parse(localStorage.getItem('payType')).sign;
			        const payType = JSON.parse(localStorage.getItem('payType')).payType;
			        this.active = payType
			        if (payType == '1') {
			          this.wxMerchant = sign
			        } else {
			          this.zfbMerchant = sign
			        }
			      }
			      this.$nextTick(() => {
			        // this.showEyeValueFn()
			      })
			    },
			
			// 当前的提交的form表单
			currentFormFn(payType) {
				if (payType == '1' && this.wxMerchant == '2') {
					return [{
						wxInfo: {
							...this.wxForm,
							id: this.wxPayId ? this.wxPayId : '',
							merchantId: this.merchantId,
							type: (this.payType == '1' && this.wxMerchant == '2') ? this.payType : '',
							sign: (this.payType == '1' && this.wxMerchant == '2') ? this.wxMerchant : ''
						},
					}, 'wxInfo']
				} else if (payType == '1' && this.wxMerchant == '1') {
					return [{
							wxSpInfo: {
								...this.wxServiceForm,
								id: this.wxSpPayId ? this.wxSpPayId : '',
								merchantId: this.merchantId,
								type: (this.payType == '1' && this.wxMerchant == '1') ? this.payType : '',
								sign: (this.payType == '1' && this.wxMerchant == '1') ? this.wxMerchant : ''
							},
						},
						'wxSpInfo'
					]
				} else if (payType == '2' && this.zfbMerchant == '2') {
					return [{
						zfbInfo: {
							...this.zfbForm,
							id: this.zfbPayId ? this.zfbPayId : '',
							merchantId: this.merchantId,
							type: (this.payType == '2' && this.zfbMerchant == '2') ? this.payType : '',
							sign: (this.payType == '2' && this.zfbMerchant == '2') ? this.zfbMerchant : ''
						},
					}, 'zfbInfo']
				} else {
					return [{
						zfbSpInfo: {
							...this.zfbServiceForm,
							id: this.zfbSpPayId ? this.zfbSpPayId : '',
							merchantId: this.merchantId,
							type: (this.payType == '2' && this.zfbMerchant == '1') ? this.payType : '',
							sign: (this.payType == '2' && this.zfbMerchant == '1') ? this.zfbMerchant : ''
						},
					}, 'zfbSpInfo']
				}
			},
		}
	}
</script>

<style>
	.sumbit-btn {
		width: 670rpx;
		position: fixed;

		bottom: 50rpx;

		height: 100rpx;
		color: #fff;
		border-radius: 10px;
		background-color: #1890FF;
	}

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		padding: 30rpx;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>